package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.MedicalExamineItem;
import cn.edu.lzu.service.MedicalExamineItemService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: MedicalExamineItemController
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/3/26 14:15
 * @Version: 1.0
 */
@Controller
public class MedicalExamineItemController {
    @Resource
    private MedicalExamineItemService medicalExamineItemService;

    @ResponseBody
    @RequestMapping("/getMedExamineItems")
    public DataPage getMedExamineItems(int currentPageNum, int pageSize){
        Session session= SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        DataPage dataPage=medicalExamineItemService.getMedicalExamineItemList(examiner.getUnitId(), currentPageNum, pageSize);
        return dataPage;
    }

    @ResponseBody
    @RequestMapping("/deleteMedItemById")
    public ResponseResult deleteMedItemById(Integer examineItemId){
        ResponseResult responseResult=medicalExamineItemService.deleteMedItemById(examineItemId);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/submitAddMedExamineItem")
    public ResponseResult submitAddMedExamineItem(MedicalExamineItem medicalExamineItem){
        Session session= SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        medicalExamineItem.setUnitId(examiner.getUnitId());
        ResponseResult responseResult=medicalExamineItemService.addMedExamineItem(medicalExamineItem);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/getAllMedItems")
    List<MedicalExamineItem> getAllMedItems(){
        Session session= SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        List<MedicalExamineItem> medicalExamineItemList=medicalExamineItemService.getAllMedItems(examiner.getUnitId());
        return medicalExamineItemList;
    }


}
